package eteam.aps.controller;

import eteam.aps.common.R;
import eteam.aps.common.component.LoginUserRedisService;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.exceptions.AuthException;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.model.entity.DataDictionary;
import eteam.aps.model.entity.DataDictionaryCategory;
import eteam.aps.model.vo.DataDictionaryCategoryTreeVo;
import eteam.aps.model.vo.DataDictionaryCategoryUpdateVo;
import eteam.aps.model.vo.DataDictionaryVo;
import eteam.aps.service.DataDictionaryCategoryService;
import eteam.aps.service.DataDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典控制层
 * 
 * @author WMT
 */
@Api(tags = "数据字典模块")
@RestController
public class DataDictionaryController implements IDataDictionaryController {

	@Autowired
	private DataDictionaryCategoryService dataDictionaryCategoryService;

	@Autowired
	private DataDictionaryService dataDictionaryService;

	@Autowired
	private LoginUserRedisService loginUserRedisService;

	@Override
	@ApiOperation("新增字典")
	@PostMapping("/dd/insert")
	public R<Integer> insert(@RequestBody DataDictionary dictionary) throws Exception, AuthException {
		if (Helper.isEmpty(dictionary.getfDdcId())) {
			throw new BusinessException(HttpStatus.BAD_REQUEST, "请先选择字典类别");
		}
		if (Helper.isEmpty(dictionary.getfDdCode()) || Helper.isEmpty(dictionary.getfDdName())) {
			throw new BusinessException(HttpStatus.BAD_REQUEST, "字典编号或字典名称不能为空");
		}
		DataDictionaryCategoryUpdateVo categoryDb = dataDictionaryCategoryService.selectUpdate(dictionary.getfDdcId());
		if (categoryDb == null) {
			throw new BusinessException(HttpStatus.BAD_REQUEST, "该字典类别不存在");
		}
//		if (!categoryDb.getfDdcManagerId().equals(loginUserRedisService.getUser().getFuserid())){
//			throw new BusinessException(HttpStatus.BAD_REQUEST,"字典分类管理人与字典录入人不一致");
//		}
		if (dataDictionaryService.selectCountByFDdCode(dictionary.getfDdCode()) > 0) {
			throw new BusinessException(HttpStatus.BAD_REQUEST, "该字典编码已经存在");
		}
		if (dataDictionaryService.selectCountByFDdName(dictionary.getfDdName()) > 0) {
			throw new BusinessException(HttpStatus.BAD_REQUEST, "该字典名称已经存在");
		}
		return R.success(dataDictionaryService.insert(dictionary));
	}

	@Override
	@ApiOperation("删除字典")
	@PostMapping("/dd/delete/{fId}")
	public R<Integer> delete(@PathVariable String fId) throws Exception, AuthException {
		if (Helper.isEmpty(fId)) {
			throw new BusinessException(HttpStatus.BAD_REQUEST, "没有字典内码");
		}
		DataDictionaryVo dictionaryDb = dataDictionaryService.selectById(fId);
		if (dictionaryDb == null){
			throw new BusinessException(HttpStatus.BAD_REQUEST,"字典不存在");
		}
		if (dataDictionaryService.selectFStateByFId(fId) == ParentEntity.FSTATE_DELETED){
			throw new BusinessException(HttpStatus.BAD_REQUEST,"字典已被删除");
		}
		return R.success(dataDictionaryService.delete(fId));
	}

	@Override
	@ApiOperation("更新字典")
	@PostMapping("/dd/update")
	public R<Integer> update(@RequestBody DataDictionary dictionary) throws Exception, AuthException {
		if (Helper.isEmpty(dictionary.getfDdcId())) {
			throw new BusinessException(HttpStatus.BAD_REQUEST, "请先选择字典类别");
		}
		if (Helper.isEmpty(dictionary.getfDdCode()) || Helper.isEmpty(dictionary.getfDdName())) {
			throw new BusinessException(HttpStatus.BAD_REQUEST, "字典编号或字典名称不能为空");
		}
		DataDictionaryCategory categoryDb = dataDictionaryCategoryService.selectByFId(dictionary.getfDdcId());
		if (categoryDb == null) {
			throw new BusinessException(HttpStatus.BAD_REQUEST, "该字典类别不存在");
		}
		DataDictionaryVo dictionaryDb = dataDictionaryService.selectByfId(dictionary.getfId());
		if (dictionaryDb == null) {
			throw new BusinessException(HttpStatus.BAD_REQUEST, "该字典不存在");
		}
		if (!dictionaryDb.getfDdCode().equals(dictionary.getfDdCode())) {
			if (dataDictionaryService.selectCountByFDdCode(dictionary.getfDdCode()) > 0) {
				throw new BusinessException(HttpStatus.BAD_REQUEST, "该字典编码已经存在");
			}
		}
		if (!dictionaryDb.getfDdName().equals(dictionary.getfDdName())) {
			if (dataDictionaryService.selectCountByFDdName(dictionary.getfDdName()) > 0) {
				throw new BusinessException(HttpStatus.BAD_REQUEST, "该字典名称已经存在");
			}
		}
		return R.success(dataDictionaryService.update(dictionary));
	}

	@Override
	@ApiOperation("分页查询字典")
	@PostMapping("/dd/selectAll")
	public R<JsonPage<DataDictionaryVo>> selectAll(@RequestParam("fDdcId") String fDdcId,
                                                   @RequestParam(value = "fDdCode", required = false) String fDdCode,
                                                   @RequestParam(value = "fDdName", required = false) String fDdName, @RequestParam("page") Integer page,
                                                   @RequestParam("pageSize") Integer pageSize) throws Exception, AuthException {
		if ((fDdcId = Helper.trim(fDdcId)) == null) {
			throw new IllegalArgumentException("请选择数据字典类别");
		}
		Map<String, Object> map = new HashMap<String, Object>(4);
		map.put("fDdcId", fDdcId);
		if (Helper.isNotEmpty(fDdCode)) {
			map.put("fDdCode", Helper.likeSql(fDdCode));
		}
		if (Helper.isNotEmpty(fDdName)) {
			map.put("fDdName", Helper.likeSql(fDdName));
		}
		return R.success(dataDictionaryService.selectAll(map, Helper.valuePositive(page, 1),
				Helper.valuePositive(pageSize, 10)));
	}

	@Override
	@ApiOperation("查询字典详情")
	@PostMapping("/dd/selectDetails/{fId}")
	public R<DataDictionaryVo> selectDetails(@PathVariable String fId) throws Exception, AuthException {
		if (Helper.checkObjAllFieldsIsNull(dataDictionaryService.selectByfId(fId))) {
			throw new BusinessException(HttpStatus.BAD_REQUEST, "fId不存在");
		}
		return R.success(dataDictionaryService.selectByfId(fId));
	}


	@Override
	@ApiOperation("查询字典分类目录树")
	@PostMapping("/dd/selectFDdcTree")
	public List<DataDictionaryCategoryTreeVo> selectFDdcTree() throws Exception, AuthException {
		return dataDictionaryCategoryService.selectFDdcTree();
	}
}
